package com.corpgovernment.organization.service.impl;

import com.corpgovernment.api.organization.model.org.OrgConfig;
import com.corpgovernment.api.organization.model.org.OrgConfigInfoVo;
import com.corpgovernment.api.organization.model.org.request.GetOrgConfigBatchRequest;
import com.corpgovernment.api.organization.model.org.request.GetOrgConfigRequest;
import com.corpgovernment.api.organization.model.org.request.SaveOrgConfigRequest;
import com.corpgovernment.api.organization.model.org.response.GetOrgConfigBatchResponse;
import com.corpgovernment.api.organization.model.org.response.GetOrgConfigResponse;
import com.corpgovernment.organization.constant.CommonConst;
import com.corpgovernment.organization.entity.db.MbOrgConfig;
import com.corpgovernment.organization.entity.db.MbOrgInfo;
import com.corpgovernment.organization.mapper.MbOrgConfigMapper;
import com.corpgovernment.organization.service.CommonService;
import com.corpgovernment.organization.service.IOrgConfigService;
import com.corpgovernment.organization.service.OrganizationBaseService;
import com.corpgovernment.organization.util.JsonUtil;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 组织设置服务接口实现
 *
 * @author dlz
 * @since 2023/5/5
 */
@Service
@Slf4j
public class OrgConfigServiceImpl extends OrganizationBaseService implements IOrgConfigService {
    @Autowired
    private MbOrgConfigMapper orgConfigMapper;
    @Autowired
    private CommonService commonService;

    /**
     * 保存组织设置
     */
    @Override
    public void saveOrgConfig(SaveOrgConfigRequest saveOrgConfigRequest) {
        String orgId = saveOrgConfigRequest.getOrgId();
        MbOrgConfig record = new MbOrgConfig();
        record.setOrgId(orgId);
        record.setConfigInfoJson(JsonUtil.toJson(saveOrgConfigRequest.getOrgConfigInfo()));
        log.warn("保存组织设置:{}", JsonUtils.toJsonString(record));
        if (hasOrgConfig(orgId)) {
            // 构建查询条件
            Example example = new Example(MbOrgConfig.class);
            example.createCriteria().andEqualTo("orgId", orgId).andEqualTo("isDeleted", 0);
            orgConfigMapper.updateByExampleSelective(record, example);
        } else {
            orgConfigMapper.insertSelective(record);
        }
    }

    @Override
    public void deleteOrgConfig(List<String> orgIds) {
        // 构建查询条件
        Example example = new Example(MbOrgConfig.class);
        example.createCriteria().andEqualTo("isDeleted", 0).andIn("orgId", orgIds);

        // 设置更新值
        MbOrgConfig record = new MbOrgConfig();
        record.setIsDeleted(true);

        orgConfigMapper.updateByExampleSelective(record, example);
    }



    /**
     * 获取组织设置
     */
    @Override
    public GetOrgConfigResponse getOrgConfig(GetOrgConfigRequest getOrgConfigRequest) {
        MbOrgConfig record = new MbOrgConfig();
        record.setOrgId(getOrgConfigRequest.getOrgId());
        record.setIsDeleted(false);
        MbOrgConfig mbOrgConfig = orgConfigMapper.selectOne(record);

        GetOrgConfigResponse response = new GetOrgConfigResponse();
        if (mbOrgConfig != null) {
            response.setOrgConfigInfo(JsonUtil.toObject(mbOrgConfig.getConfigInfoJson(), OrgConfigInfoVo.class));
            return response;
        }

        // 数据库没查到则返回默认值
        OrgConfigInfoVo orgConfigInfo = new OrgConfigInfoVo();
        orgConfigInfo.setCostCenterIsRequired(CommonConst.FALSE);
        orgConfigInfo.setProjectIsRequired(CommonConst.FALSE);
        orgConfigInfo.setNameEditDuringBooking(false);

        response.setOrgConfigInfo(orgConfigInfo);
        return response;
    }

    /**
     * 是否有组织设置
     */
    private boolean hasOrgConfig(String orgId) {
        MbOrgConfig record = new MbOrgConfig();
        record.setOrgId(orgId);
        record.setIsDeleted(false);
        return orgConfigMapper.selectCount(record) > 0;
    }

    @Override
    public GetOrgConfigBatchResponse getOrgConfigBatch(GetOrgConfigBatchRequest request) {
        if (CollectionUtils.isEmpty(request.getOrgIdList())) {
            return null;
        }
        Example example = new Example(MbOrgConfig.class);
        example.createCriteria().andIn("orgId", request.getOrgIdList());
        List<MbOrgConfig> mbOrgConfigList = orgConfigMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(mbOrgConfigList)){
            return null;
        }
        GetOrgConfigBatchResponse response = new GetOrgConfigBatchResponse();
        ArrayList<OrgConfig> orgConfigs = new ArrayList<>();
        request.getOrgIdList().forEach(e ->{
            OrgConfig orgConfig = new OrgConfig();
            orgConfig.setOrgId(e);
            MbOrgConfig mbOrgConfig = mbOrgConfigList.stream().filter(k -> e.equals(k.getOrgId())).findFirst().orElse(null);
            if (Objects.nonNull(mbOrgConfig)){
                orgConfig.setOrgConfigInfo(JsonUtil.toObject(mbOrgConfig.getConfigInfoJson(), OrgConfigInfoVo.class));
            } else {
                OrgConfigInfoVo orgConfigInfo = new OrgConfigInfoVo();
                orgConfigInfo.setCostCenterIsRequired(CommonConst.FALSE);
                orgConfigInfo.setProjectIsRequired(CommonConst.FALSE);
                orgConfigInfo.setNameEditDuringBooking(false);
                orgConfig.setOrgConfigInfo(orgConfigInfo);
            }
            orgConfigs.add(orgConfig);
        });
        response.setOrgConfigList(orgConfigs);
        return response;
    }
}
